<template>
	<view class="material-view">
		<view class="tabs-box">
			<scroll-view scroll-x style="100%">
				<view class="fx-center">
					<view class="tab-item" :class="tabIndex === -1 ? 'tab-item-active' : ''" @click="changeTab(-1)">全部
					</view>
					<view class="tab-item" :class="tabIndex === index ? 'tab-item-active' : ''"
						v-for="(item,index) in tabs" :key="index" @click="changeTab(index)">{{item.name}}</view>
				</view>
			</scroll-view>
		</view>

		<view class="list-box">
			<view class="list-item" v-for="item in list" :key="item.id" @click="toDetail(item.id)" v-if="list.length>0">
				<image :src="item.cover_url" mode="aspectFill" class="list-img"></image>
				<view class="list-other">
					<view class="list-name">{{item.title}}</view>
					<view class="fx-center">
						<view class="main-tag-item" v-for="tag in item.tags" :key="tag.id">{{tag.name}}
						</view>
					</view>
				</view>
			</view>
			<view class="main-no-data" v-else>
				<image src="/static/empty_img.png" mode="widthFix"></image>
				<view class="">暂无素材</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [],
				tabIndex: -1,
				list: [],
				pid: null,
				total: 0,
				page: 1
			};
		},
		onLoad(e) {
			if (e.pid) {
				this.pid = e.pid
			}
			this.getType()
			this.getList()
		},
		onReachBottom() {
			if (this.total < this.list.length) {
				this.page++
				this.getList()
			}
		},
		methods: {
			getType() {
				this.$api.requst(`/gwc/projects/material_types`, {
					type: 1
				}).then((res) => {
					this.tabs = res.data
				})
			},
			getList() {
				this.$api.requst(`/gwc/projects/${this.pid}/materials`, {
					page: this.page,
					type_id: this.tabIndex == -1 ? '' : this.tabs[this.tabIndex].id,
					type: 1,
					id: this.pid
				}).then((res) => {
					this.total = res.total
					if (this.page === 1) {
						this.list = res.data
					} else {
						res.data.forEach(item => {
							this.list.push(item)
						})
					}
				})
			},
			changeTab(index) {
				this.tabIndex = index
				this.page = 1
				this.getList()
			},
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/workbench/material/detail/detail?id=' + id
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.material-view {
		.tabs-box {
			margin: 16rpx 32rpx 36rpx;
			overflow: auto;

			.tab-item {
				font-size: 30rpx;
				color: #5C5F66;
				margin-right: 64rpx;
				white-space: nowrap;
			}

			.tab-item-active {
				font-weight: bold;
				color: #161616;
			}
		}

		.list-box {
			// display: flex;
			// flex-wrap: wrap;
			// justify-content: space-between;
			margin: 0 8rpx 0;
			clear: both;

			.list-item {
				margin-bottom: 8rpx;
				border-radius: 4rpx;
				overflow: hidden;
				width: 362rpx;

				.list-img {
					width: 362rpx;
					height: 484rpx;
				}

				.list-other {
					padding: 16rpx;
					background-color: #fff;

					.list-name {
						font-size: 32rpx;
						color: #161616;
					}
				}

			}

			.list-item:nth-child(2),
			.list-item:nth-child(2n+3) {
				float: right;
			}

			.list-item:nth-child(1),
			.list-item:nth-child(2n+4) {
				float: left;
			}

			.list-item:last-child {
				padding-bottom: 50rpx;
			}
		}

		.main-tag-item {
			background-color: #F1F3F6;
			color: #8A8F99;
			margin-top: 8rpx;
		}
	}
</style>